<?php
include_once "xAP-functions/webincludes.php";
echo webpageheader();

// add item
if ($_POST["add"]!=""){
	$sql="insert into xAP_Event 
	(Alias,Active) VALUES
	('".dbesc($_POST["add"])."',1)";
	dbexec($sql);

}
if ($_GET["delete"]!=""){
	$id=intval($_GET["delete"]);
	dbexec("delete from xAP_EventElement where EventID=".$id);
	dbexec("delete from xAP_EventLink where EventID=".$id);
	dbexec("delete from xAP_Event where EventID=".$id);
}

$now=date("Y-m-d H:i:s");

$page=0;

$sql="select EventID,Alias,Active 
from xAP_Event 
order by Alias";

$results=dbquery($sql);

//display
?>
<script>
  function Delete(id){
	if(confirm('Remove Event?')){
		location.href ='index.php?page=<?=$page?>&delete=' + id;
	}
  }
  function RunEvent(id){
	  $.get("runevent.php?id=" + id);
  }
</script>
<?php
echo "<table width=100%>
<TR><TD></td><TD></td><TD></td><TD class='boldtext'>Event</td><td></td></tr>";
foreach($results as $row){
	echo "<tr>
	<TD class='normaltext'>
	<a href='event_active.php?id=".$row["EventID"]."&state=".$row["Active"]."&page=".$page."'>
	<img src='".LayoutOnOffIcon($row["Active"])."'></a>
	</td>
	<TD class='normaltext'>
	<a href='#' OnClick=\"Delete(".$row["EventID"].")\">
	<img src='/xAP-Layout/".$config["theme"]."/images/delete.gif'>
	</a>
	</td>
	<TD class='normaltext'>
	<a href='#' OnClick=\"RunEvent(".$row["EventID"].")\">
	<img src='/xAP-Layout/".$config["theme"]."/images/run.png'>
	</a>
	</td>
	<TD class='normaltext' width=100%><a href='event_edit.php?id=".$row["EventID"]."&page=".$page."'>".$row["Alias"]."</a></td>
	<TD class='normaltext'>
	<a href='event_edit.php?id=".$row["EventID"]."&page=".$page."'>Edit</a>
	</td>
	</tr>";
	echo "<TR bgcolor='#dddddd'><td colspan=5 height=1></td></tr>";
}


echo "<form method='post' action='index.php'>
<TR><TD></td><TD></td><TD></td><TD class='boldtext'><input name='add' size=30><input type='submit' value='Add'></td><td></td></tr>
</form>
</table>";

echo webpagefooter();
?>